package com.sunday.player;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.sunday.player.adapter.adapter_musiclist;
import com.sunday.player.domain.musiclist;
import com.sunday.player.interfaceutil.interface_AdapterCallback;
import com.sunday.player.util.AlertBoxColtroler;
import com.sunday.player.util.SundaySharedPreferences;
import com.sunday.player.util.playerControl;
import com.sunday.player.util.searchMusicfile;


/**
 * 조건 
 * 1. MP3 목록이 없을경우 기본으로 구성한다 .
 * 2. 무조건 DB 에서 읽어온다 
 * 3. 종료시 서비스에 리스트 업뎃.
 * 
 * 
 * @author user
 *
 */
public class Activity_listEditer extends basicActivity implements OnClickListener{
    private searchMusicfile searchMusicfile;
    private ListView musicListView ;
    private List<musiclist> mulist = new ArrayList<musiclist>();;
    private adapter_musiclist a_Musiclist;
    private Thread worker = new Thread();
    private interface_AdapterCallback adapterCallback = new interface_AdapterCallback() {
        
        @Override
        public void onItemClickCallback(int Position, int State, int Action) {
            playerControl.Trace_i("onItemClickCallback = "+State+" - "+Action, TAG);
            if(!Doublechecker()){
        	return ;
            }
            
            
            	if(mulist.get(Position).getMusiclist().size() == 0){
            	    return;
            	}
            	try {
        	    
            	  //  PlayerControler.getMusicList().addAll(mulist.get(Position).getMusiclist());
        	    Playlist_Insert(mulist.get(Position));
        	    Toast.makeText(Activity_listEditer.this, 
        		  getString(R.string.Toast_Mess_Add_ListAll)
        		    , Toast.LENGTH_SHORT).show();
        	} catch (RemoteException e) {
        	    // TODO Auto-generated catch block
        	    e.printStackTrace();
        	}
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_listedit);
        super.onCreate(savedInstanceState);
        musicListView = (ListView)findViewById(R.id.listView1);
        findViewById(R.id.imageButton_add).setOnClickListener(this);
        findViewById(R.id.imageButton_delete).setOnClickListener(this);
        findViewById(R.id.imageButton_move).setOnClickListener(this);
        findViewById(R.id.imageButton_rename).setOnClickListener(this);
        a_Musiclist = new adapter_musiclist(this, 0, mulist , adapterCallback);
        musicListView.setAdapter(a_Musiclist);
        listRefrash();

        /**
         * 최초구동시 mp3 구성으로 이동
         */
        //if(sundaySharedPreferences.getFirstMp3listLoad()){
        if(mulist.size() == 0){
            searchMusicfile = new searchMusicfile(this);
            searchMusicfile.searchAddMP3();
            sundaySharedPreferences.setSetting(SundaySharedPreferences.firstMp3listLoad, true);
        }else{
        }
    }
    
    
    @Override
    public void onScreenUpdate() {
	listRefrash();
    }
    /**
     * DB 에서 리스트 불러오기
     */
    public void listRefrash(){
	mulist.clear();
	mulist.addAll(getdBcontroler().list_Call_All());
	
	Runnable show = new Runnable() {
	    
	    @Override
	    public void run() {
		a_Musiclist.notifyDataSetChanged();
		hideProgressBar(false);
	    }
	};
	defaultHandler.post(show);
    }


    @Override
    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.imageButton_add:
	    alertColtroler.showDialog(AlertBoxColtroler.Show_MakeList);
	    break;
	case R.id.imageButton_delete:{
	    showProgerssBar("삭제중입니다");
	    boolean isdel = false;
	    for(musiclist item : mulist){
		if(item.isCheck()){
		    getdBcontroler().list_delete(item);
		    isdel = true;
		}
	    }
	    if(isdel){
		listRefrash();
	    }
	    
	}break;
	case R.id.imageButton_rename:
	    
	    break;

	default:
	    break;
	}
    }
    
}
